var ajax = {
	list : function(option,data) {
		if (option.dialog) {
			var dialogs = [];
			if ($("#" + option.dialog).length == 0) {
				dialogs.push(option.dialog);
			}
			if (dialogs.length > 0) {
				dialog.loadDep(dialogs, option, ajax.list);
				return;
			}
		}
		var deps = ajax.getDepTemp(option);
		if (deps.length > 0) {
			template.loadDep(deps, option, ajax._list);
		} else {
			ajax._list(option);
		}
	},
	_list : function(option,data) {
		if (!option.pager) {
			$("#" + option.dialog + " [name='pageMode.currentPage']").val(1);
		}
		var subData = null;
		if (typeof (option.data) == 'string') {
			subData = $("#" + option.data).serialize();
		} else if (typeof (option.data) == 'object') {
			subData = option.data;
		}
		$.post(option.url, subData, function(data) {

			var dig = $("#" + option.dialog);
			var target;
			if (option.target) {
				target = $("#" + option.target);
			} else {
				target = dig;
			}
			if ($.isEmptyObject(data.actionErrors)) {
				if (option.template) {
					$("#pagerTemp").template("pagerTemp");
					$("#" + option.template).tmpl(data)
							.appendTo(target.empty());
				}
				if (option.callback) {
					$.each(option.callback,function(i,n){
						n(option,data);
					});
				}
				$('.btn').button().removeClass('btn');
				dig.dialog("open");
			} else {
				commons.actionError(option,data);
			}
		}, "json");
	},
	submit : function(option,data) {
		$.post(option.url, option.data, function(data) {
			if ($.isEmptyObject(data.fieldErrors)
					&& $.isEmptyObject(data.actionErrors)) {
				if (option.callback) {
					$.each(option.callback,function(i,n){
						n(option,data);
					});
					$("#" + option.dialog).dialog("close");
				}
				if (!$.isEmptyObject(data.actionMessages)) {
					alert(data.actionMessages.join(','));
				}
			} else {
				commons.fieldError(option,data);
				commons.actionError(option,data);
			}
		}, "json");
	},
	load : function(option,data) {
		var dialogs = [];
		if ($("#" + option.dialog).length == 0) {
			dialogs.push(option.dialog);
		}
		if (dialogs.length > 0) {
			dialog.loadDep(dialogs, option, ajax.load);
			return;
		}
		var deps = ajax.getDepTemp(option);
		if (deps.length > 0) {
			template.loadDep(deps, option, ajax._load);
		} else {
			ajax._load(option);
		}
	},
	_load : function(option,data) {
		$.post(option.url, option.data, function(data) {
			if ($.isEmptyObject(data.actionErrors)) {
				if (option.template) {
					$("#" + option.template).tmpl(data).appendTo(
							$("#" + option.dialog).empty());
				}
				$('.btn').button().removeClass('btn');
				$("#" + option.dialog).dialog("open");
				if(option.callback){
					$.each(option.callback,function(i,n){
						n(option,data);
					});
				}
			} else {
				commons.actionError(option,data);
			}
		}, "json");
	},
	del : function(option,data) {
		$.post(option.url, option.data, function(data) {
			if ($.isEmptyObject(data.actionErrors)) {
				if (option.callback) {
					$.each(option.callback,function(i,n){
						n(option,data);
					});
				}
			} else {
				commons.actionError(option,data);
			}
		}, "json");
	},
	getDepTemp : function(option,data) {
		var depsTemp = [];
		if (option.dep) {
			if (typeof (option.dep) == "string") {
				depsTemp.push(option.dep);
			} else if (typeof (option.dep) == "array") {
				$.each(option.dep,function(i,n){
					depsTemp.push(n);
				});
			}
		}
		if (option.template) {
			depsTemp.push(option.template);
			depsTemp.push("pagerTemp");
		}
		var deps = [];
		for ( var i = 0; i < depsTemp.length; i++) {
			if (!$('#' + depsTemp[i]).is('script')) {
				deps.push(depsTemp[i]);
			}
		}
		return deps;
	},
	next : function(option,data) {
		var currentPage=$("#"+option.dialog+" [name='pageMode.currentPage']");
		var currentPageVal=parseInt(currentPage.val());
		currentPageVal+=1;
		currentPage.val(currentPageVal);
		option.pager=true;
		commons.list(option);
	},
	pre : function(option) {
		var currentPage=$("#"+option.dialog+" [name='pageMode.currentPage']");
		var currentPageVal=parseInt(currentPage.val());
		currentPageVal-=1;
		currentPage.val(currentPageVal);
		option.pager=true;
		commons.list(option);
	},
	fileUpload : function(option,data) {
		var process=$('#fileUploadProcess');
		if(!process.is('form')){
			var dia=['fileUploadProcess'];
			dialog.loadDep(dia,option,ajax.fileUpload);
			return;
		}
		$('#'+option.dialog).ajaxSubmit({
			url:option.url,
		    beforeSend: function() {
		    	process.dialog('open');
		    	process.children(".percent").progressbar({
					value: 0
				});
		    },
		    uploadProgress: function(event, position, total, percentComplete) {
		    	process.children(".percent").progressbar({
					value: percentComplete
				});
		    },
			success:function(xhr){
				process.dialog('close');
				var data;
				if(typeof(xhr)=="object"){
					data=xhr;
				}else{
					data=$.parseJSON(xhr);
				}
				if($.isEmptyObject(data.actionErrors)){
					$('#'+option.dialog).dialog('close');
					if(option.callback){
						$.each(option.callback,function(i,n){
							n(option);
						});
					}
				}else{
					commons.actionError(option,data);
				}
			}
		}); 
	}
};
var commons = {
	submit:function(option,data){
		$("#" + option.dialog + " .err").html('');
		var subData = $("#" + option.dialog).serialize();
		option.data = subData;
		if ($("#" + option.dialog + " [type='file']").length > 0) {
			ajax.fileUpload(option);
		} else {
			ajax.submit(option);
		}
	},
	list : function(option,data) {
		ajax.list({
			url : option.modeName+"Action_listPage.action",
			dialog :  option.modeName+"SearchList",
			data :  option.modeName+"SearchList",
			target :  option.modeName+"SearchList .list",
			template :  option.modeName+"ListTemp",
			pager:option.pager
		});
	},
	modify : function(option,data) {
		ajax.load({
			url : option.modeName+"Action_load.action",
			dialog : option.modeName+"Update",
			data : commons.initPara(option,data),
			template : option.modeName+"UpdateTemp",
			callback:option.callback
		});
	},
	detail : function(option,data) {
		ajax.load({
			url :  option.modeName+"Action_load.action",
			dialog :  option.modeName+"Detail",
			data : commons.initPara(option,data),
			template : option.modeName+"DetailTemp"
		});
	},
	copy : function(option,data) {
		ajax.load({
			url :  option.modeName+"Action_load.action",
			dialog :  option.modeName+"Add",
			data : commons.initPara(option,data),
			template : option.modeName+"CopyTemp"
		});
	},
	del : function(option,data) {
		if (confirm('\u60a8\u786e\u5b9a\u8981\u5220\u9664\uff1f')) {
			ajax.del({
				url :  option.modeName+"Action_delete.action",
				data : commons.initPara(option,data),
				modeName:option.modeName,
				callback : [commons.list]
			});
		}
	},
	next : function(btn,option,data) {
		$(btn).attr("disabled","disabled");
		ajax.next( {'dialog':option.modeName+"SearchList",'modeName':option.modeName});
	},
	pre : function(btn,option) {
		$(btn).attr("disabled","disabled");
		ajax.pre( {'dialog':option.modeName+"SearchList",'modeName':option.modeName});
	},
	openDia : function(option,data) {
		var deps = ajax.getDepTemp(option);
		if (deps.length > 0) {
			template.loadDep(deps, option, commons.openDia);
			return ;
		}
		var dialogs=[];
		if($("#" + option.dialog).length==0){
			dialogs.push(option.dialog);
		}
		if(dialogs.length>0){
			dialog.loadDep(dialogs,option,commons.openDia);
			return;
		}else{
			$("#" + option.dialog).dialog('open');
		}
		if(option.callback){
			$.each(option.callback,function(i,n){
				n(option);
			});
		}
	},
	fieldError : function(option,data) {
		if (!$.isEmptyObject(data.fieldErrors)) {
			for ( var key in data.fieldErrors) {
				var error = $("#" + option.dialog + " .err" + "[title='"
						+ key + "']");
				if (!error.is('span')) {
					error = $('<span>').addClass('err').attr('title', key);
					error.insertAfter($("#" + option.dialog + " [name='"
							+ key + "']"));
				}
				error.html(data.fieldErrors[key].join(','));
			}
		}

	},
	actionError : function(option,data) {
		if (!$.isEmptyObject(data.actionErrors)) {
			alert(data.actionErrors.join(','));
		}
	},
	initPara:function(option,data){
		var da;
		if(option.data){
			da=option.data;
		}else{
			da = {
				"t.id" : option.id
			};
		}
		return da;
	}
};